package com.csii.ssm.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ui.Model;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * 日期工具类
 *
 * @author zhaojin 15398699939@163.com
 * @create 2016-03-26-下午8:15
 */

public class DateUtil {

    private static Logger logger = LoggerFactory.getLogger(DateUtil.class);

    public static final SimpleDateFormat DATE_FORMATTER = new SimpleDateFormat("yyyy-MM-dd");
    public static final SimpleDateFormat DATE_TIME_FORMATTER = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public static boolean compareToLessZhanCurrentDateTimeAWeek(Date date) {
        Date curDate = new Date();
        return curDate.getTime() - date.getTime() >= 604800000;
    }

    public static boolean compareToLessZhanAWeek(Date date, Date compareDate) {
        return date.getTime() - compareDate.getTime() >= 604800000;
    }

    /**
     * 查看给定时间到当前日期间隔了几周
     * 多出1天都加1在返回结果上
     * @param beginDatetime
     * @return
     */
    public static int getIntervalWeeksCompareToCurrentDate(Date beginDatetime) {
        Date curDate = new Date();
        float tmp = curDate.getTime() - beginDatetime.getTime();
        return (int) Math.ceil(tmp / 604800000);
    }

    public static String NowDate(){
        return DATE_FORMATTER.format(new Date());
    }

    public static String getYesterdayStr(){
        return DATE_FORMATTER.format(getYesterday());
    }
    public static Date getYesterday(){
        Calendar calendar = new GregorianCalendar();
        Date date=new Date();//取时间
        calendar.setTime(date);
        calendar.add(calendar.DATE,-1);//把日期往后增加一天.整数往后推,负数往前移动
        date=calendar.getTime(); //重新获取时间
        return date;
    }
    public static String getTomorrowStr(){
        return DATE_FORMATTER.format(getTomorrow());
    }
    public static Date getTomorrow(){
        Calendar calendar = new GregorianCalendar();
        Date date=new Date();//取时间
        calendar.setTime(date);
        calendar.add(calendar.DATE,1);//把日期往后增加一天.整数往后推,负数往前移动
        date=calendar.getTime(); //这个时间就是日期往后推一天的结果
        return date;
    }

    public static String NowTime(){
        return DATE_TIME_FORMATTER.format(new Date());
    }

    /**
     * 字符串转换成日期
     * @param str
     * @return date
     */
    public static Date StrToDate(String str) {

        Date date = null;
        try {
            date = DATE_FORMATTER.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
        return date;
    }

    /**
     * 设置查询条件开始结束日期默认值
     * @param model
     * @return
     */
    public static void setDefaultDateRange(Model model)
    {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar= Calendar.getInstance();
        calendar.add(Calendar.MONTH,-1);//开始日期默认为一个月前
        Date date=calendar.getTime();
        String beginDate=sdf.format(date);
        String endDate=NowDate();
        model.addAttribute("beginDate",beginDate);
        model.addAttribute("endDate", endDate);
    }

    /**
     * 获取当日日期
     * @return
     */
    public static String getCurrDateStr()
    {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");
        Date date=new Date();
        return sdf.format(date);
    }


    /**
     * 获取当日日期
     * @return
     */
    public static String getCurrDate()
    {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar= Calendar.getInstance();
        Date date=calendar.getTime();
        return sdf.format(date);
    }

    public static void main(String[] args) {
        System.out.println(getYesterday());
        System.out.println(getTomorrow());
        System.out.println(StrToDate("2016-05-10"));
    }
}
